<?php


namespace app\web\controller;

use app\api\controller\v2\Auth;
use app\common\lib\Aes;
use app\common\lib\exception\ApiException;
use app\common\model\Recode;
use think\Cache;
use think\Controller;
use think\Env;

/**
 * 星座卡片
 * Class AstroCard
 * @package app\web\controller
 */
class AstroCard extends Controller
{

    protected $test = false;

    protected $LOG_PRE = '星座卡片web错误：';

    /**
     * 获取星座卡片数据
     * @return mixed
     * @throws \Exception
     */
    public function index()
    {
        if (!$this->request->isGet())
        {
            return show(config('code.error'), '未知操作');
        }

        $rid = input('rid/d', 0);

        if ($this->test === false)
        {
            if (!$rid)
            {
                return show(config('code.error'), '当前档案不存在');
            }
            //校验token
            try
            {
                $auth = new Auth();
                $uid = $auth->user['id'];
            }
            catch (\Exception $e)
            {
                throw new ApiException('星座卡片错误：权限认证失败,请稍后重试！');
            }
        } else
        {
            $uid = 12346392;
            $rid = 4922636;
        }

        $recode = (new Recode())->getRecode(['id' => $rid, 'uid' => $uid]);

        $res = (new \app\web\service\AstroCard())->getCardData($recode);

//        $res = Cache::get('web_astro_card_' . $rid);
//        if ($res === false)
//        {
//            $res = (new \app\web\service\AstroCard())->getCardData($recode);
//            Cache::set('web_astro_card_' . $rid, json_encode($res, true), 86400);
//        } else
//        {
//            $res = json_decode($res, true);
//        }
        $host = Env::get('host.photo');

        $power = [];
        foreach ($res['power'] as $key => $re)
        {
            $num = str_split($re['score']);
            $len = count($num);
            $re['score1'] = "";
            $re['score2'] = "";
            $re['score3'] = "";
            if ($re['id'] === 0)
            {
                $re['score1'] = $host . "web/static/astro_explain/images/$num[0]-red.png";
                if ($len > 1)
                {
                    $re['score2'] = $host . "web/static/astro_explain/images/$num[1]-red.png";
                }
                if ($len > 2)
                {
                    $re['score3'] = $host . "web/static/astro_explain/images/$num[2]-red.png";
                }
            } elseif ($re['id'] === 1)
            {
                $re['score1'] = $host . "web/static/astro_explain/images/$num[0]-orange.png";
                if ($len > 1)
                {
                    $re['score2'] = $host . "web/static/astro_explain/images/$num[1]-orange.png";
                }
                if ($len > 2)
                {
                    $re['score3'] = $host . "web/static/astro_explain/images/$num[2]-orange.png";
                }
            } elseif ($re['id'] === 2)
            {
                $re['score1'] = $host . "web/static/astro_explain/images/$num[0]-blue.png";
                if ($len > 1)
                {
                    $re['score2'] = $host . "web/static/astro_explain/images/$num[1]-blue.png";
                }
                if ($len > 2)
                {
                    $re['score3'] = $host . "web/static/astro_explain/images/$num[2]-blue.png";
                }
            }
            $power[] = $re;
        }
        $data = [
            'user_info' => [
                'avatar' => $res['avatar'] ? $host . $res['avatar'] : "https://oss.goddessxzns.com/avatar_default_2.png",
                'recode_des' => $res['name'],
                'surface_des' => "表面：{$res['surface']}",
                'actual_des' => "实则：{$res['inherent']}",
            ],
            'attr_score' => $res['attr'],
            'ability_score' => $power,
            'Love_concept' => $res['love_concept'],
            'unique_skills' => [
                'title' => $res['skill'],
                'list' => $res['keyword']
            ],
        ];
        return $this->fetch('index', ['host' => $host, 'data' => $data]);
    }


    /**
     * 获取星座卡片数据（星座卡片分享，android 11 以上，微博分享的是一个url，其余的 分享的是图片，和2.7.3 一起上）
     * @return mixed
     * @throws \Exception
     */
    public function share()
    {
        if (!$this->request->isGet())
        {
            return show(config('code.error'), '未知操作');
        }

        $token_param = input('token/s', '');
        $isEncode = input('is_encode/d', 0);
        if (!$isEncode)
        {
            $token = urldecode($token_param);
            //把空格替换成“+”
            $token = str_replace(' ', '+', $token);
        } elseif ($isEncode === 2)
        {
            $token = base64_decode($token_param, true);
        } else
        {
            $token = $token_param;
        }
        // access-token AES解密
        $str = (new Aes())->decryptWithOpenssl($token);
        if (empty($str))
        {
            throw new ApiException($this->LOG_PRE . '参数错误');
        }
        parse_str($str, $params);

        //检查输入参数是否正确
        if (!isset($params['uid']))
        {
            throw new ApiException($this->LOG_PRE . 'uid参数为空');
        }
        if (!isset($params['rid']))
        {
            throw new ApiException($this->LOG_PRE . 'rid参数为空');
        }

        $recode = (new Recode())->getRecode(['uid' => $params['uid'], 'id' => $params['rid']]);
        //oss资源地址
        $oss = Env::get('host.photo');

        $astroExplainData = (new \app\web\service\AstroExplain())->getAstroExplain($recode);

        $smrData = $astroExplainData['smrData'];
        $data = $astroExplainData['data'];
        return $this->fetch('index_edition_for_two', ['smrData' => $smrData, 'oss' => $oss, 'data' => $data]);
    }
}
